package com.zntg.pdf;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
import com.zntg.bo.FundFeneSureBook;
import com.zntg.utils.DateUtil;
import com.zntg.utils.FileUtils;
import com.zntg.utils.MoneyTool;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
 * @program: 对应ods库T_SPCI_PROD这张表的SPCI_TYE = '3'的份额确认书生成功能
 * @description:
 * @author: zhengyunfei
 * @create: 2018-12-03 09:20
 **/
public class CreatePdfUtilTemplate3 {
    public static void main(String[] args) throws Exception {

    }
    /**
    * @Description: 非净值类产品
    * @Author: zhengyunfei
    * @Date: 2019/4/12
    * @time:17:49
    */
    public static FundFeneSureBook createPdf(FundFeneSureBook fundFeneSureBook) throws IOException, DocumentException {
        // pdf模板
      /*  String fileName = "D:\\work\\ylcaifu\\智能投顾二期\\报表平台\\pdf生成好的不带logo的模板\\优策月月盈3号份额确认书-（追加申购模板）2.pdf";*/
        String fileName=fundFeneSureBook.getSourcePath();
        PdfReader reader = new PdfReader(fileName);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        /* 将要生成的目标PDF文件名称 */
        PdfStamper ps = new PdfStamper(reader, bos);
        PdfContentByte under = ps.getUnderContent(1);
        /* 使用中文字体 */
        BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
        fontList.add(bf);
        /* 取出报表模板中的所有字段 */
        AcroFields fields = ps.getAcroFields();
        fields.setSubstitutionFonts(fontList);
        fillData(fields, fillTemplateData(fundFeneSureBook));
        /* 必须要调用这个，否则文档不会生成的 */
        ps.setFormFlattening(true);
        ps.close();
        //获取当天日期
        String today= DateUtil.getToDay();
        String l_storageid=fundFeneSureBook.getlStorageid();
        String targetPath=fundFeneSureBook.getTargetPath();
        String path=targetPath+today;
        boolean flag=FileUtils.mkdir(path);
        if(!flag){
            path=path+"\\Tblobstorage";
         //   file=new File(path);
            flag=FileUtils.mkdir(path);
        }else{
            path=path+"\\Tblobstorage";
        }
        String lStorageId=l_storageid.substring(0,l_storageid.length()-1);
        path=path+"\\"+lStorageId;
        FileUtils.mkdir(path);
        String createTofileName=l_storageid+"baccessory";
        FileUtils.createParentDir(path+"\\"+createTofileName);
        //生成pdf路径
        OutputStream fos = new FileOutputStream(path+"\\"+createTofileName);
        fos.write(bos.toByteArray());
        fos.flush();
        fos.close();
        bos.close();
        File f=new File(path+"\\"+createTofileName);
        fundFeneSureBook.setFileSize(f.length()+"");
        System.out.println("生成认购书生成成功。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。"+path+"\\"+createTofileName);
        return fundFeneSureBook;
    }
     /**
     * 填充模板
     * */
    public static void fillData(AcroFields fields, Map<String, String> data)
            throws IOException, DocumentException {
        for (String key : data.keySet()) {
            String value = data.get(key);
            if (!key.startsWith("Image")){
                //设置文本域参数值
                fields.setField(key, value); // 为字段赋值,注意字段名称是区分大小写的
            }else{
                //将图片写入指定的field
                      /*   Image image = Image.getInstance("D:\\work\\learn\\itext-master\\seal.png");
                         //将图片写入指定的field
                         PushbuttonField pb = fields.getNewPushbuttonFromField(key);
                         pb.setImage(image);
                         fields.replacePushbuttonField(key, pb.getField());*/

            }
        }
    }
    /**
     * 其中data存放的key值与pdf模板中的文本域值相对应
     * 非净值类产品专用
     * */
    public static Map<String, String> fillTemplateData(FundFeneSureBook fundFeneSureBook) {
        Map<String, String> data = new HashMap<String, String>();
        data.put("userName", fundFeneSureBook.getUserName());
        String htNo=fundFeneSureBook.getHtNo();
        data.put("bianhao", htNo);//合同编号
        data.put("prodNm", fundFeneSureBook.getProdNm());//产品名称
        data.put("year", fundFeneSureBook.getIntDateY());//份额确认日 年
        data.put("month",fundFeneSureBook.getIntDateM());//份额确认日 月
        data.put("day", fundFeneSureBook.getIntDateD());//份额确认日 日
        //基金成立日
        data.put("fundCreateY", fundFeneSureBook.getFundCreateDateY());//基金成立日 年
        data.put("fundCreateM",fundFeneSureBook.getFundCreateDateM());//基金成立日 月
        data.put("fundCreateD", fundFeneSureBook.getFundCreateDateD());//基金成立日 日
        //小写金额转大写金额
        String bigMoney=fundFeneSureBook.getRgBigMoney();
        if(""!=bigMoney&&null!=bigMoney){
            double moneyDouble=Double.parseDouble(bigMoney);
            String MONEY=MoneyTool.change(moneyDouble);
            if(MONEY.contains("元整")){
                MONEY=MONEY.replace("元整","");
            }
            if(MONEY.contains("万")){
                MONEY=MONEY.replace("万","");
            }
            if(MONEY.contains("千")){
                MONEY=MONEY.replace("千","仟");
            }
            data.put("MONEY",MONEY);
        }
        //存续期限
        data.put("cDeadLine",fundFeneSureBook.getcDeadLine());
        data.put("money", MoneyTool.formatTosepara(Double.parseDouble(fundFeneSureBook.getRgSmallMoney())));//消息金额千分位分隔
        //确认书生成日，底部盖章日期，需要转换为大写
        data.put("createDateY",fundFeneSureBook.getCreateDateYear());
        data.put("createDateM",fundFeneSureBook.getCreateDateMth());
        data.put("createDateD",fundFeneSureBook.getCreateDateDay());
        return data;
    }

}
